using System;
using System.Collections;
using System.Text;
using System.Data;

namespace RestService.Processing
{
    public class InformationRoom : DoiTuong
    {
        #region string RoomName property
        public string RoomName
        {
            get
            {
                if (_DataRow["RoomName"] is string) return (string)_DataRow["RoomName"];
                return "";
            }
            set
            {
                _DataRow["RoomName"] = value;
            }
        }
        #endregion

        #region DateTime? StartDate property
        public DateTime? StartDate
        {
            get
            {
                if (_DataRow["StartDate"] is DateTime) return (DateTime)_DataRow["StartDate"];
                return null;
            }
            set
            {
                _DataRow["StartDate"] = value;
            }
        }
        #endregion

        #region DateTime? EndDate property
        public DateTime? EndDate
        {
            get
            {
                if (_DataRow["EndDate"] is DateTime) return (DateTime)_DataRow["EndDate"];
                return null;
            }
            set
            {
                _DataRow["EndDate"] = value;
            }
        }
        #endregion

        #region string Infor property
        public string Infor
        {
            get
            {
                if (_DataRow["Infor"] is string) return (string)_DataRow["Infor"];
                return "";
            }
            set
            {
                _DataRow["Infor"] = value;
            }
        }
        #endregion
        public InformationRoom(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucInformationRoom : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucInformationRoom _Instance;
        public static DanhMucInformationRoom Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucInformationRoom();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "InformationRoom";
            }
        }
        #endregion 

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new InformationRoom((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public InformationRoom this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new InformationRoom(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public InformationRoom NewDoiTuong()
        {
            try
            {
                return new InformationRoom(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public InformationRoom GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new InformationRoom(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public InformationRoom GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucInformationRoom GetAll() {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is Exception) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucInformationRoom Filter(string filterExpression)
        {
            try
            {
                DanhMucInformationRoom obj = new DanhMucInformationRoom();
                obj.DataView = new DataView(DanhMucInformationRoom.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
